<?php

/**
 * Menashop
 *
 * LICENSE
 *
 * This source file is subject to the license that is bundled
 * with this project in the file LICENSE.txt
 *
 * @category   Zend
 * @package    Admin_Model
 * @subpackage Distributor
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 * @version    1.0.0
 * @since      File available since Release 1.0.0
 */

/**
 * The domain for distributor
 * 
 * @category   Zend
 * @package    Admin_Model
 * @subpackage Distributor
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 */
class Admin_Model_Distributor extends Zend_Db_Table_Abstract
{

    /**
     * The name of table
     * @var string 
     */
    protected $_name = 'distributor';

    /**
     * Save distributor
     * @param array $row
     */
    public function save($row)
    {
        // check ID (insert or update)
        $id = isset($row['id']) ? $row['id'] : null;
        if ($id) {
            unset($row['id']);
            $this->update($row, array('id = ?' => $id));
        } else {
            $id = $this->insert($this->_name, $row);
        }
        return $id;
    }

    /**
     * Find distributor ID by name
     * @param string $name
     * @return integer
     */
    public function findIdByName($name)
    {
        // find distributor by name in database
        $query = $this->getAdapter()->select();
        $query->from($this->_name, 'id');
        $query->where('name = ?', $name);
        $result = $this->getAdapter()->fetchOne($query);
        return $result;
    }

    /**
     * Get distributors by filter 
     * @return array 
     */
    public function getAllByFilter($id, $name, $manager, $email, $phone, $currencyId, $sort, $direction, $page, $rowCount)
    {
        $query = $this->getAdapter()->select();
        $query->from(array('d' => $this->_name), array('d.id', 'd.name', 'd.manager', 'd.email', 'd.phone'));
        $query->join(array('c' => 'currency'), 'd.currency_id = c.id', 'c.code as currency_code');

        // Check filters
        if ($id) {
            $query->where('d.id = ?', $id);
        }
        if ($name) {
            $query->where('d.name LIKE ?', $name . '%');
        }
        if ($manager) {
            $query->where('d.manager LIKE ?', $manager . '%');
        }
        if ($email) {
            $query->where('d.email = ?', $email);
        }
        if ($phone) {
            $query->where('d.phone = ?', $phone);
        }
        if ($currencyId) {
            $query->where('d.currency_id = ?', $currencyId);
        }

        if ($sort && $direction) {
            $query->order($sort . ' ' . $direction);
        }

        $query->limitPage($page, $rowCount);

        // Get distributors from DB
        $result = $this->getAdapter()->fetchAll($query, null, Zend_Db::FETCH_OBJ);
        return $result;
    }

    /**
     * Get count of distributor by filter 
     * @return integer 
     */
    public function getCountByFilter($id, $name, $manager, $email, $phone, $currencyId, $sort, $direction, $page, $rowCount)
    {
        $query = $this->getAdapter()->select();
        $query->from(array('d' => $this->_name), new Zend_Db_Expr('COUNT(*)'));

        // Check filters
        if ($id) {
            $query->where('d.id = ?', $id);
        }
        if ($name) {
            $query->where('d.name LIKE ?', $name . '%');
        }
        if ($manager) {
            $query->where('d.manager LIKE ?', $manager . '%');
        }
        if ($email) {
            $query->where('d.email = ?', $email);
        }
        if ($phone) {
            $query->where('d.phone = ?', $phone);
        }
        if ($currencyId) {
            $query->where('d.currency_id = ?', $currencyId);
        }

        if ($sort && $direction) {
            $query->order($sort . ' ' . $direction);
        }

        // Get count of distributors from DB
        $result = $this->getAdapter()->fetchOne($query);

        return $result;
    }

    /**
     * Get distributor by ID
     * @param integer $id
     * @return Admin_Model_Distributor 
     */
    public function getById($id)
    {
        $query = $this->getAdapter()->select();
        $query->from($this->_name, array('id', 'name', 'manager', 'phone', 'email', 'currency_id'));
        $query->where('id = ?', $id);

        // Get distributor from DB
        $result = $this->getAdapter()->fetchRow($query, null, Zend_Db::FETCH_OBJ);
        return $result;
    }

}
